﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Facade;
using System.Data;
using System.Globalization;

namespace IronOne.SalesOne.SalesOneWeb.SalesActivity
{
    public partial class ActivityPlan : SOSecurityPage
    {
        string SOCode;
        StructureFacade facade;

        protected void Page_Load(object sender, EventArgs e)
        {
            facade = new StructureFacade();
            SOCode = Request.QueryString["SOCode"];

            SetCredentials(ValidateUserCredentials(Request.Url.LocalPath));

            if (string.IsNullOrEmpty(SOCode))
                Response.Redirect("~/SalesActivity/SearchActivityPlan.aspx");

            if (!Page.IsPostBack)
            {
                DateTime dt = DateTime.Parse(Request.QueryString["from"]);
                DateTimeFormatInfo info = new DateTimeFormatInfo();

                lblHeader.Text = "Monthly View " + info.MonthNames[dt.Month - 1] + " " + dt.Year + " for " + SOCode;
                try
                {
                    populateActivityPlanGrid();
                }
                catch
                {
                    MassageSummery("IIS514");
                }
            }
        }

        private void SetCredentials(StringCollection credential)
        {
            foreach (string item in credential)
            {
                switch (item.ToLower())
                {
                    //case "c":
                    //    btnUpdate.Visible = true;
                    //    break;
                    case "u":
                        btnUpdate.Visible = true;
                        break;
                    case "d":
                        btnClear.Visible = true;
                        break;
                    /*case "v":
                        btnView.Visible = true;
                        break;*/
                    default:
                        break;
                }
            }
        }

        private void populateActivityPlanGrid()
        {
            ActivityPlanGrid.DataSource = facade.GetActivityPlan(DateTime.Parse(Request.QueryString["from"]), DateTime.Parse(Request.QueryString["to"]), SOCode);
            ActivityPlanGrid.DataBind();
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                string dates = getSelectedDates();
                if (!string.IsNullOrEmpty(dates))
                {
                    Response.Redirect("~/SalesActivity/UpdateRemark.aspx?dates=" + dates + "&SOCode=" + SOCode + "&from=" + Request.QueryString["from"] + "&to=" + Request.QueryString["to"] + "&a=u");
                }
                else
                {
                    //showMessage("Please select a date first.",true);
                    MassageSummery("IIS503");
                }
            }
            catch
            {
                MassageSummery("IIS514");
            }
        }

        /*private void showMessage(string msg, bool error)
        {
            lblMsg.Text = msg;
            if (error)
                lblMsg.ForeColor = System.Drawing.Color.Red;
            else
                lblMsg.ForeColor = System.Drawing.Color.Green;
        }*/

        protected void btnClear_Click(object sender, EventArgs e)
        {
            try
            {
                string dates = getSelectedDates();
                if (!string.IsNullOrEmpty(dates))
                {
                    foreach (string date in dates.Split(','))
                    {
                        facade.ClearSupervisorRemark(DateTime.Parse(date.Split('.')[0]), SOCode);
                    }
                    populateActivityPlanGrid();
                }
                else
                {
                    //showMessage("Please select a date first.", true);
                    MassageSummery("IIS503");
                }
            }
            catch
            {
                MassageSummery("IIS514");
            }
        }

        private string getSelectedDates()
        {
            string dates = string.Empty;

            foreach (GridViewRow row in ActivityPlanGrid.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("CB");

                if (cb != null)
                {
                    if (cb.Checked == true)
                    {
                        Label lc = (Label)row.FindControl("lblRemark");
                        string flag = "." + (lc == null ? "0" : (string.IsNullOrEmpty(lc.Text) ? "1" : "0"));  //if 1, insert
                        dates += (cb.Checked == true ? (dates.EndsWith(",") ? row.Cells[1].Text : "," + row.Cells[1].Text) : string.Empty) + flag;
                    }
                }
            }
            return string.IsNullOrEmpty(dates) ? dates : dates.Substring(1);
        }
        
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/SalesActivity/SearchActivityPlan.aspx");
        }
    }
}
